package com.dbf.forum.controller.user;



import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dbf.forum.Constants;
import com.dbf.forum.model.UserDetails;
import com.dbf.forum.security.CurrentUser;
import com.dbf.forum.user.model.User;
import com.dbf.forum.user.service.UserService;

import javax.inject.Inject;


@RestController
@RequestMapping(value = Constants.URI_API + Constants.URI_SELF)
public class CurrentUserController {

    private static final Logger log = LoggerFactory
            .getLogger(CurrentUserController.class);
    
    @Inject
    private UserService userService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public UserDetails currentUser(@CurrentUser UserDetails model) {
    	if (log.isDebugEnabled()) {
            log.debug("get current user info");
        }
    	User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        String name = user.getUsername(); //get logged in username
//        model.addAttribute("username", name);
    	UserDetails userDetails = new UserDetails();
    	userDetails.setUsername(user.getUsername());
    	userDetails.setName(user.getName());
    	userDetails.setEmail(user.getEmail());
    	userDetails.setRole(user.getRole());
//        UserDetails details = userService.getEntity(User.class, "username", user.getUsername());
        if (log.isDebugEnabled()) {
            log.debug("current user value @" + userDetails);
        }


        return userDetails;
    }

//    @RequestMapping(value = "", method = RequestMethod.PUT, params = "aciton=CHANGE_PWD")
//    @ResponseBody
//    public ResponseEntity<Void> changePassword(
//            @CurrentUser User user,
//            @RequestBody PasswordForm fm) {
//        if (log.isDebugEnabled()) {
//            log.debug("change password of user@" + fm);
//        }
//
//        userService.updatePassword(user.getId(), fm);
//
//        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
//    }
//
//    @RequestMapping(value = "", method = RequestMethod.PUT, params = "action=UPDATE_PROFILE")
//    @ResponseBody
//    public ResponseEntity<Void> updateProfile(
//            @CurrentUser User user,
//            @RequestBody ProfileForm fm) {
//        if (log.isDebugEnabled()) {
//            log.debug("update user profile data @" + fm);
//        }
//
//        userService.updateProfile(user.getId(), fm);
//
//        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
//    }

}
